﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System;


namespace carXapp2.classes
{
    public class CarStats
    {
        private float _overallMPG;
        private int _overallMiles;
        private float _overallExpenditureForFuel;
        private float _overallExpenditureForMaint;
        private int _mpgCalcMethod=1;
        private ObservableCollection<fuelInfo> fuelRecords;
        private ObservableCollection<maintInfo> maintRecords;

        public CarStats(ObservableCollection<fuelInfo> f)
        {
            this.fuelRecords = f;
        }

        public CarStats(ObservableCollection<maintInfo> m)
        {
            this.maintRecords = m;
        }

        public CarStats(ObservableCollection<maintInfo> m,ObservableCollection<fuelInfo> f)
        {
            this.fuelRecords = f;
            this.maintRecords = m;
        }

        public double GetOverallMPG(int mpgmethod)
        {
            int count=0;
            fuelInfo oldRec = new fuelInfo();
            int baseMiles=0; 
            float _overallFilled=0;
            switch (mpgmethod)
            {
                case 1:
                foreach (fuelInfo newRec in fuelRecords)
                {
                    if (count != 0)
                    {
                        baseMiles += (newRec.Miles - oldRec.Miles);
                        _overallFilled += oldRec.Filled;
                    }
                    oldRec = newRec;
                    count++;
                    _overallMPG = baseMiles / _overallFilled;
                }
                break;
                case 2:
                
                foreach (fuelInfo newRec in fuelRecords)
                {
                    if (count != 0)
                    {
                        baseMiles += (newRec.Miles - oldRec.Miles);
                        _overallFilled += newRec.Filled;
                    }
                    oldRec = newRec;
                    count++;
                    _overallMPG = baseMiles / _overallFilled;
                }
                break;

           
        }
            double allMpg = double.Parse(_overallMPG.ToString());
            allMpg = Math.Round(allMpg, 2);
            if (allMpg > 100 || Double.IsNaN(allMpg) || Double.IsInfinity(allMpg))
            {
                allMpg = 0;
            }
            return allMpg;
        }

        public float[] GetMPGListbyMonth(int method)
        {
            int count = 0;
            int[] miles = new int[13];
            float[] vol = new float[13];
            float[] mpg = new float[13];
            fuelInfo oldRec = new fuelInfo();
            int CurYear = DateTime.Now.Year;
            int recMonth;
            switch (method)
            {
                case 1:
                    foreach (fuelInfo newRec in fuelRecords)
                    {
                        recMonth = newRec.Date.Month;
                        if (newRec.Date.Year == CurYear)
                        {
                            if (count != 0)
                                { 
                                miles[newRec.Date.Month] += newRec.Miles - oldRec.Miles;
                                vol[newRec.Date.Month] += oldRec.Filled;
                                }
                                
                            
                        }
                        oldRec = newRec;
                        count++;
                        
                    }
                    count = 0;
                    float TmpMpg;
                    foreach (int mile in miles)
                    {
                        if (mile != 0 && vol[count] != 0 )
                        {
                            try
                            {
                                
                                TmpMpg = mile / vol[count];
                                if (float.IsInfinity(TmpMpg) || float.IsNaN(TmpMpg) || TmpMpg > 100)
                                {
                                    TmpMpg = 0f;
                                }
                                mpg[count] = TmpMpg;
                            }
                            catch (DivideByZeroException)
                            {
                                mpg[count] = 0;
                            }
                        }
                        else
                        {
                            mpg[count] = 0;
 
                        }
                        count++;
                    }
                                
                    break;
                case 2:
                 foreach (fuelInfo newRec in fuelRecords)
                    {
                        recMonth = newRec.Date.Month;
                        if (newRec.Date.Year == CurYear)
                        {
                            if (count != 0)
                                { 
                                miles[newRec.Date.Month] += newRec.Miles - oldRec.Miles;
                                vol[newRec.Date.Month] += newRec.Filled;
                                }
                                
                            
                        }
                        oldRec = newRec;
                        count++;
                        
                    }
                    count = 0;
                    foreach (int mile in miles)
                    {
                        if (mile != 0 && vol[count] !=0)
                        {
                            try
                            {
                                TmpMpg = mile / vol[count];
                                if (float.IsInfinity(TmpMpg) || float.IsNaN(TmpMpg) || TmpMpg > 100)
                                {
                                    TmpMpg = 0f;
                                }
                                mpg[count] = TmpMpg;
                            }
                            catch (DivideByZeroException)
                            {
                                mpg[count] = 0;
                            }
                        }
                        else
                        {
                            mpg[count] = 0;
 
                        }
                        count++;
                    }
                                
                    break;
            }
           

            return mpg;
        }

        public int GetTotalMiles()
        {
            int count = 0;
            fuelInfo oldRec = new fuelInfo();
            foreach (fuelInfo newRec in fuelRecords)
            {
                if (count != 0)
                { _overallMiles += newRec.Miles - oldRec.Miles; }
                oldRec = newRec;
                count++;
            }
            return _overallMiles;
 
        }

        public float GetTotalFuelCost()
        {
            foreach (fuelInfo newRec in fuelRecords)
            {
                _overallExpenditureForFuel += newRec.Cost;
            }
            return _overallExpenditureForFuel;
         
        }

        public int GetTotalDays()
        {
            if (fuelRecords.Count > 0)
            {
                DateTime today = DateTime.Now;
                var firstDate = fuelRecords.First().Date;
                var days = today - firstDate;
                var rdays = days.Days;
                return int.Parse(rdays.ToString());

            }
            else
            {
                return 0;
            }
        }

        public float GetTotalMaintCost()
        {
            foreach (maintInfo newRec in maintRecords)
            {
                _overallExpenditureForMaint += newRec.TotalCost;
            }
            return _overallExpenditureForMaint;

        }

        public float GetTotalLoborCost()
        {
            float cost=0;
            foreach (maintInfo newRec in maintRecords)
            {
                cost += newRec.LaborCharges;
            }
            return cost;
 
        }

        public float GetTotalPartsCost()
        {
            float cost = 0;
            foreach (maintInfo newRec in maintRecords)
            {
                cost += newRec.PartCost;
            }
            return cost; 

        }

        public float[] GetTotalFuelCostByMonth()
        {
            float[] cost = new float[13];

            foreach (fuelInfo info in fuelRecords)
            {
                if (info.Date.Year == DateTime.Now.Year)
                {
                    cost[info.Date.Month] += info.Cost;
                }
            }
            return cost;
        }

        public float[] GetMaintCostByMonth()
        {
            float[] cost = new float[13];
            foreach (maintInfo info in maintRecords)
            {
                if (info.Date.Year == DateTime.Now.Year)
                {
                    cost[info.Date.Month] += info.TotalCost;
                }
            }
            return cost;
        }

        public float[] GetMaintLaborCostByMonth()
        {
            float[] cost = new float[13];
            foreach (maintInfo info in maintRecords)
            {
                if (info.Date.Year == DateTime.Now.Year)
                {
                    cost[info.Date.Month] += info.LaborCharges;
                }
            }
            return cost;
        }
        public float[] GetMaintPartCostByMonth()
        {
            float[] cost = new float[13];
            foreach (maintInfo info in maintRecords)
            {
                if (info.Date.Year == DateTime.Now.Year)
                {
                    cost[info.Date.Month] += info.PartCost;
                }
            }
            return cost;
        }

        public double[] GetMPG(int method)
        {
            double[] mileage = new double[fuelRecords.Count];
            fuelInfo oldRec = new fuelInfo();
            int miles;
            float volume;
            int count = 0;
            double TmpMpg;
            if (method == 1)
            {
                foreach (fuelInfo newRec in fuelRecords)
                {
                    miles = newRec.Miles - oldRec.Miles;
                    volume = oldRec.Filled;
                    try
                    {

                         TmpMpg = miles / volume;
                         if (Double.IsInfinity(TmpMpg) || Double.IsNaN(TmpMpg) || TmpMpg > 100)
                         { TmpMpg = 0 ;}
                         mileage[count] = TmpMpg;
                    }
                    catch (DivideByZeroException e)
                    {
                        mileage[count] = 0;
                    }
                    if (double.IsNaN(mileage[count]))
                    {mileage[count] = 0;}
                    mileage[count] = Math.Round(mileage[count], 2);
                    count++;
                    oldRec = newRec;
                }
            }
            else if(method == 2)
            {
                foreach (fuelInfo newRec in fuelRecords)
                {
                    miles = newRec.Miles - oldRec.Miles;
                    volume = newRec.Filled;
                    try
                    {
                        TmpMpg = miles / volume;
                        if (Double.IsInfinity(TmpMpg) || Double.IsNaN(TmpMpg) || TmpMpg > 100)
                        { TmpMpg = 0; }
                        mileage[count] = TmpMpg;

                    }
                    catch (DivideByZeroException e)
                    {
                        mileage[count] = 0;
                    }
                    if (double.IsNaN(mileage[count]))
                    { mileage[count] = 0; }
                    mileage[count] = Math.Round(mileage[count], 2);
                    count++;
                    oldRec = newRec;
                }
            }
           
            return mileage;
            
        }

    }
}
